/**
 * Created by Jaye on 2018/2/26.
 */
jQuery(document).ready(function () {
    //依赖于jQuery的代码
    // jQuery('#confirmPayModal').on('show.bs.modal', function () {
    //
    // });
});
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
function confirmOrder() {
    var order_form = jQuery('#order_form').serialize();
    jQuery.ajax(
        {
            url: "/order",
            data: order_form,
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.code == 1000) {
                    swal({
                        title: "下单成功",
                        type: "success",
                        confirmButtonText: "确认",
                        closeOnConfirm: true
                    }, function () {
                        jQuery('#payModal').modal('hide');
                        jQuery('#tradeId').val(data.data.tradeId);
                        jQuery('#payCommodityName').val(data.data.commodityName);
                        jQuery('#payPrice').val(data.data.payAmount);
                        jQuery('#confirmPayModal').modal('show');
                        return;
                    });
                } else {
                    swal({
                        title: "下单失败",
                        type: "error",
                        buttons: false,
                        timer: 1000
                    });
                    return;
                }
            },
            error: function () {
                jQuery("#tip").html("<span style='color:red'>，请重试</span>");
            }
        });
}
function confirmPay() {
    var pay_form = jQuery('#pay_form').serialize();
    jQuery.ajax(
        {
            url: "/pay",
            data: pay_form,
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.code == 1000) {
                    swal({
                        title: "支付成功",
                        type: "success",
                        confirmButtonText: "确认",
                        closeOnConfirm: false
                    }, function () {
                        jQuery('#confirmPayModal').modal('hide');
                        window.location.href="/index";
                    });
                    return;
                } else {
                    swal({
                        title: "支付失败",
                        type: "error",
                        buttons: false,
                        timer: 1000
                    });
                    return;
                }
            },
            error: function () {
                swal({
                    title: "支付发生错误，请刷新重试！",
                    type: "error",
                    buttons: false,
                    timer: 1000
                });
                return;;
            }
        });
}


